﻿using System;
using System.Web.UI.WebControls;

namespace ECommerce.Controls
{
	public partial class Comments : BaseUserControl
	{
		private Service.IProductReviewService _ProductReviewService;

		public int ProductID
		{
			get
			{
				return CommonHelper.QueryStringInt("productid");
			}
		}

		protected void Page_init(object sender, EventArgs e)
		{
			ButtonAddReview.Text = GetLocalResourceString("ProductReview.AddReviewButtonText");
		}

		protected void Page_Load(object sender, EventArgs e)
		{
		}

		protected void ButtonAddReview_Click(object sender, EventArgs e)
		{
			if (SiteContext.Current == null)
			{
				Server.Transfer("~/HomePage.aspx");
			}

			if (TextReviewContent.Text.Trim().Length > 0)
			{
				var theReview = new Data.Entity.ProductReview();

				theReview.CreatedOn = DateTime.Now;
				theReview.HelpfulNoTotal = 0;
				theReview.HelpfulYesTotal = 0;
				theReview.IPAddress = SiteContext.Current.IPAddress;
				theReview.IsApproved = false;
				theReview.ProductID = ProductID;
				theReview.Rating = 0;
				theReview.ReviewText = TextReviewContent.Text.Trim();
				theReview.Title = string.Empty;
				theReview.UserID = SiteContext.Current.User != null ? (Guid)SiteContext.Current.User.ProviderUserKey : Guid.Empty;

				if(_ProductReviewService == null)
				{
					_ProductReviewService = new Service.ProductReviewService();
				}

				_ProductReviewService.CreateNewProductReview(theReview);

				TextReviewContent.Text = string.Empty;
			}
		}

		protected void RepeaterProductReviews_ItemDataBound(object sender, RepeaterItemEventArgs e)
		{
			if(e.Item.DataItem != null)
			{
				var data = e.Item.DataItem as Data.Entity.ProductReview;

				var LiteralDatetime = e.Item.FindControl("LiteralDatetime") as Literal;
				var LiteralUserName = e.Item.FindControl("LiteralUserName") as Literal;
				var LiteralReviewContent = e.Item.FindControl("LiteralReviewContent") as Literal;
				var LiteralReviewReply = e.Item.FindControl("LiteralReviewReply") as Literal;
				var LiteralReviewReplyDate = e.Item.FindControl("LiteralReviewReplyDate") as Literal;

				var productreviewreply = e.Item.FindControl("productreviewreply") as System.Web.UI.HtmlControls.HtmlControl;

				LiteralDatetime.Text = data.CreatedOn.ToString();
				LiteralReviewContent.Text = data.ReviewText;

				if(data.UserID != Guid.Empty)
				{
					var user = System.Web.Security.Membership.GetUser(data.UserID);

					LiteralUserName.Text = user.UserName;
				}
				else
				{
					LiteralUserName.Text = data.IPAddress.Split('.')[0] + "." + data.IPAddress.Split('.')[1] + ".***.***";
				}

				if(!string.IsNullOrEmpty(data.ReplyText))
				{
					productreviewreply.Visible = true;
					LiteralReviewReply.Text = data.ReplyText;
					LiteralReviewReplyDate.Text = data.RepliedOn.Value.ToString();
				}
				else
				{
					productreviewreply.Visible = false;
				}
			}
		}

		protected void Page_PreRender(object sender, EventArgs e)
		{
			BindProductReviews();
		}

		private void BindProductReviews()
		{
			if(_ProductReviewService == null)
			{
				_ProductReviewService = new Service.ProductReviewService();
			}

			if(ProductID > 0)
			{
				var reviews = _ProductReviewService.GetProductReviewsByProduct(ProductID);

				RepeaterProductReviews.DataSource = reviews;
				RepeaterProductReviews.DataBind();
			}
		}
	}
}